package com.dtwenty.gaming.client;

import com.dtwenty.gaming.client.widgets.CharacterSheet;
import com.dtwenty.gaming.data.PlayerCharacter;
import com.dtwenty.gaming.data.PlayerCharacter.DataType;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class HomePage implements EntryPoint {

	private final DockPanel mainP = new DockPanel();
//	private PlayerCharacter character = PlayerCharacter.createPlayerCharacter(DataType.CLIENT_SIDE);
	private PlayerCharacter character = new PlayerCharacter();
	private LoginInfo loginInfo = null;
	private Anchor signInLink = new Anchor("Sign In");
	private Anchor signOutLink = new Anchor("Sign Out");
	private CharacterManagementPanel characterManagementPanel = null;



	public void onModuleLoad() {
		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>() {
			public void onFailure(Throwable error) {
			}

			public void onSuccess(LoginInfo result) {
				loginInfo = result;
				characterManagementPanel = new CharacterManagementPanel(loginInfo);
				loadLoginLogoutLink(loginInfo.isLoggedIn());
				RootPanel.get("main_container").add(characterManagementPanel);
			}
		});
	}

	private void loadLoginLogoutLink(boolean loggedIn) {
		Anchor link = null;
		if(loggedIn) {
			signOutLink.setHref(loginInfo.getLogoutUrl());
			link = signOutLink;
		} else {
			signInLink.setHref(loginInfo.getLoginUrl());
			link = signInLink;
		}
		RootPanel.get("login_container").add(link);
	}
}

